package org.jwcloud.project.module.zx.template.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.template.service.RptSpService;
import org.jwcloud.project.module.zx.template.service.RptTemplateService;
import org.jwcloud.project.module.zx.template.service.repository.RptSpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle",
description = "审批相关", tags = "sp")
@RestController
@RequestMapping("/rest/rptSp")
public class RptSpController extends BaseController{
	
//	@Autowired
//    private CommonService commonService;
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
	public RptSpRepository rptSpRepository;
	
	@Autowired
    public RptUtService rptUtService;
	
	@Autowired
    public RptSpService rptSpService;
	
	@GetMapping("queryDoneDeptList")
    @ApiOperation(value = "完成部门列表", notes = "")
    @ApiImplicitParams({
    })
    public Resp queryDoneDeptList(
    		Pagination pagination,
    		@RequestParam(required = false) Map<String,String> param
    		){
        return Resp.success(this.rptUtService.commonQeryCodeList_all("view_code_dety_org"));
    }

	@GetMapping("querySpPersonTree")
    @ApiOperation(value = "审批人树", notes = "")
    @ApiImplicitParams({
    })
    public Resp querySpPersonTree(
    		@RequestParam(required = false) Map<String,String> param
    		){
		List<Map<String,String>> list = this.rptSpRepository.querySpPersonTree(param);
		if(list == null || list.size() == 0) {
			return Resp.error("系统未配置审批人");
		}
		List res = new ArrayList();
		Map tempMap = new LinkedHashMap();
		int cot = 1;
		for(Map<String,String> map: list) {
			if(tempMap.containsKey(map.get("sp_dept"))) {
				Map tempInner = new HashMap() {
					{
						//this.put("id", map.get("sp_person"));
						this.put("label", map.get("sp_person_name"));
						this.put("levelState", "1");
					}
				};
				tempInner.putAll(map);
				tempInner.put("id", cot++);
				((List)tempMap.get(map.get("sp_dept"))).add(tempInner);
			}else {
				List innerList = new ArrayList();
				Map tempInner = new HashMap() {
					{
						//this.put("id", map.get("sp_person"));
						this.put("label", map.get("sp_person_name"));
						this.put("levelState", "1");
					}
				};
				tempInner.putAll(map);
				tempInner.put("id", cot++);
				innerList.add(tempInner);
				tempMap.put(map.get("sp_dept"), innerList);
				tempMap.put(map.get("sp_dept") + "_", map);
			}
		}
		Iterator it = tempMap.keySet().iterator();
		while(it.hasNext()) {
			String key = (String) it.next();
			if(key.endsWith("_")) {
				continue;
			}
			Map resMap = new LinkedHashMap();
//			resMap.put("id", ((Map)tempMap.get(key + "_")).get("sp_dept"));
			resMap.put("id", cot++);
			resMap.put("sp_dept", ((Map)tempMap.get(key + "_")).get("sp_dept"));
			resMap.put("label", ((Map)tempMap.get(key + "_")).get("sp_dept_name"));
			resMap.put("levelState", "2");
			resMap.put("children", tempMap.get(key));
			res.add(resMap);
		}
		return Resp.success(res);
    }
	
	@GetMapping("querySpList")
    @ApiOperation(value = "审批列表", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "project_name", value = "项目名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_name", value = "工作名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "任务名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person_dept_name", value = "完成部门", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person_dept", value = "完成部门id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sp_person_name", value = "审批人", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_start_time", value = "完成时间 start", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_end_time", value = "完成时间 end", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_state", value = "审批状态 0 1 2", required = false, dataType = "string", paramType = "query"),
    })
    public Resp querySpList(
    		Pagination pagination,
    		@RequestParam(required = false) Map<String,String> param
    		){
//		String flag = FmtUtil.nullRes(param
//				, "qian_id", "交换的id 前"
//				, "hou_id", "交换的id 后"
//				, "qian_idx", "交换的序号 前"
//				, "hou_idx", "交换的序号 后");
//		if(!StringUtils.isEmpty(flag)) 
//			return Resp.error(flag);
		
		
        Object sp_state = param.get("sp_state");
        if("0".equals(sp_state)) {
        	return this.rptSpService.querySpList_dsp(pagination, param);
        }else {
        	return this.rptSpService.querySpList(pagination, param);
        }
		
    }
	
	@GetMapping("querySpList_dsp")
    @ApiOperation(value = "审批列表", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "project_name", value = "项目名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_name", value = "工作名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "任务名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person_dept_name", value = "完成部门", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person_dept", value = "完成部门id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sp_person_name", value = "审批人", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_start_time", value = "完成时间 start", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_end_time", value = "完成时间 end", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_state", value = "审批状态 0 1 2", required = false, dataType = "string", paramType = "query"),
    })
    public Resp querySpList_dsp(
    		Pagination pagination,
    		@RequestParam(required = false) Map<String,String> param
    		){
		return this.rptSpService.querySpList_dsp(pagination, param);
    }
	
	@GetMapping("queryHisSpLogList")
    @ApiOperation(value = "历史审批列表", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "project_name", value = "项目名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_name", value = "工作名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "task_name", value = "任务名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person_dept_name", value = "完成部门", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_person_dept", value = "完成部门id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "done_start_time", value = "完成时间 start", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "done_end_time", value = "完成时间 end", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_state", value = "审批状态 0 1 2", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryHisSpLogList(
    		Pagination pagination,
    		@RequestParam(required = false) Map<String,String> param
    		){
        
		return this.rptSpService.queryHisSpLogList(pagination, param);
    }
	
	@PostMapping("doSp")
    @ApiOperation(value = "审批", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "sptg_content", value = "审批通过描述", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "spbtg_content", value = "审批不通过描述", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_state", value = "1通过  2驳回", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_task_id", value = "任务id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_project_id", value = "项目id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_work_id", value = "工作id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doSp(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "sp_state", "审批状态"
				, "rel_task_id", "任务编码"
				, "rel_project_id", "项目id"
				, "rel_work_id", "工作id"
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return this.rptSpService.doSp(param);// Resp.success(this.rptSpRepository.doSp(param));
		
    }
	
	@PostMapping("doSptg_bat")
    @ApiOperation(value = "批量审批通过", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "审批日志id  1;2;3;4", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_work_id", value = "工作id  1;2;3;4", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_project_id", value = "项目id 1;2;3;4", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_task_id", value = "rel_task_id 1;2;3;4", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doSptg_bat(
    		@RequestBody(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String[] ids = param.get("id").split(";"); 
		String[] rel_work_ids = param.get("rel_work_id").split(";"); 
		String[] rel_project_ids = param.get("rel_project_id").split(";");
		String rel_task_id = param.get("rel_task_id");
		String[] rel_task_ids = null;
		if(rel_task_id != null && rel_task_id.length() != 0) {
			rel_task_ids = rel_task_id.split(";");
		}
		Map daoMap = new HashMap();
		for(int i = 0; i < ids.length; i++) {
			String id = ids[i];
			String rel_work_id = rel_work_ids[i];
//			String pid = pids[i];
			daoMap.put("sp_state", "1");
			daoMap.put("id", id);
			daoMap.put("rel_work_id", rel_work_id);
			if(rel_task_ids == null) { 
				daoMap.put("rel_task_id", "-1");
			}else {
				daoMap.put("rel_task_id", rel_task_ids[i]);
			}
			
			daoMap.put("rel_project_id", rel_project_ids[i]);//审批通过不需要project_id--20201030又需要了，任务上报用
			doSp(daoMap);
		}
		
        return Resp.success("1");// Resp.success(this.rptSpRepository.doSp(param));
		
    }
	
	
	@GetMapping("querySpHis_byTask")
    @ApiOperation(value = "历史审批记录  单个任务", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "rel_task_id", value = "任务id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "rel_project_id", value = "项目id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "without_user", value = "1无视审批人（任务上报用）  已作废", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp querySpHis_byTask(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "rel_task_id", "任务编码"
				, "rel_project_id", "项目id"
				);
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
        return this.rptSpService.querySpHis_byTask(param);// Resp.success(this.rptSpRepository.doSp(param));
		
    }
	
	@GetMapping("queryRefuse")
    @ApiOperation(value = "驳回任务 列表", notes = "")
    @ApiImplicitParams({
    	//@ApiImplicitParam(name = "rel_task_id", value = "任务id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryRefuse(
    		@RequestParam(required = false) Map<String,String> param
    		){
//		String flag = FmtUtil.nullRes(param
//				, "rel_task_id", "任务编码"
//				);
//		if(!StringUtils.isEmpty(flag)) 
//			return Resp.error(flag);
		
        return this.rptSpService.queryRefuse(param);// Resp.success(this.rptSpRepository.doSp(param));
		
    }
	
}